//
//  SSTopView.swift
//  美团
//
//  Created by 叶竞泽 on 2017/5/14.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

let lineSpacing: CGFloat = 3

protocol SSTopViewDelegate: NSObjectProtocol {
    func topViewButtonClickWithCatagoryButton(sender: UIButton)
    func topViewButtonClickWithAreaButton(sender: UIButton)
    func topViewButtonClickWithSortButton(sender: UIButton)
}

class SSTopView: UIView {
    
    // MARK: - 属性
    
    /// 分类按钮
    @IBOutlet weak fileprivate var catagoryButton: UIButton!
    @IBOutlet weak fileprivate var areaButton: UIButton!
    @IBOutlet weak fileprivate var sortButton: UIButton!
    /// 代理
    weak var delegate: SSTopViewDelegate?
    /// 属性文本
    fileprivate var textStyle: NSMutableParagraphStyle {
        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpacing
        return style
    }
    /// 属性文本
    fileprivate var textAttributes: [String: Any] {
        return [NSParagraphStyleAttributeName: textStyle, NSForegroundColorAttributeName: UIColor.darkGray, NSFontAttributeName: UIFont.systemFont(ofSize: 11)]
    }
    
    // MARK: - 方法
    
    class func topView() -> SSTopView? {
        
        let topView = UINib(nibName: "SSTopView", bundle: nil).instantiate(withOwner: nil, options: nil).first as? SSTopView
        return topView
    }
    
    /// 根据信息设置button内容
    func setCatagoryButton(title: String, icon: String, hlIcon: String, subTtile: String?) {
        var attStr: NSMutableAttributedString?
        if subTtile == nil {
            attStr = NSMutableAttributedString(string: "\(title)")
        } else {
            attStr = NSMutableAttributedString(string: "\(title)\n\(subTtile!)")
        }
        let image = UIImage(named: icon)
        let hlImage = UIImage(named: hlIcon)
        catagoryButton.setImage(image, for: .normal)
        catagoryButton.setImage(hlImage, for: .selected)
        catagoryButton.setImage(hlImage, for: .highlighted)
        attStr!.addAttributes(textAttributes, range: NSRange(location: 0, length: attStr!.length))
        catagoryButton.setAttributedTitle(attStr, for: .normal)
    }
    
    // TODO: - 完成区域按钮点击设置
    func setAreaButton(title: String, icon: String, subTtile: String?) {
        
    }
    // TODO: - 完成排序按钮点击设置
    func setSortButton(title: String, icon: String, subTtile: String?) {
        
    }
    
    /// 监听分类按钮点击
    @objc private func catagoryButtonCilick(sender: UIButton) {
        delegate?.topViewButtonClickWithCatagoryButton(sender: sender)
    }
    /// 监听区域按钮点击
    @objc private func cityButtonCilick(sender: UIButton) {
        delegate?.topViewButtonClickWithAreaButton(sender: sender)
    }
    /// 监听排序按钮点击
    @objc private func sortButtonCilick(sender: UIButton) {
        delegate?.topViewButtonClickWithSortButton(sender: sender)
    }
    
    // MARK: - 生命周期
    
    override func awakeFromNib() {
        super.awakeFromNib()
        catagoryButton.addTarget(self, action: #selector(catagoryButtonCilick(sender:)), for: .touchUpInside)
        areaButton.addTarget(self, action: #selector(cityButtonCilick(sender:)), for: .touchUpInside)
        sortButton.addTarget(self, action: #selector(sortButtonCilick(sender:)), for: .touchUpInside)
    }
}
